from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from inference import load_model, process_input

app = FastAPI()

print("正在加载模型...")
model, tokenizer = load_model()

class ChatRequest(BaseModel):
    image_path: str
    prompt: str

@app.post("/chat")
async def chat(request: ChatRequest):
    try:
        response = process_input(model, tokenizer, request.image_path, request.prompt)
        return {"response": response}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000) 